package entity

import (
	"time"
)

var timeLayout = "2006-01-02 15:04:05"

// Response 根据antd标准返回格式 详情参考 https://pro.ant.design/zh-CN/docs/request
type Response struct {
	Success      bool        `json:"success" required:"true"`
	ErrorMessage string      `json:"errorMessage,omitempty"`
	Data         interface{} `json:"data,omitempty"`
	Total        int         `json:"total,omitempty"`
}

// Pagination 分页标准返回格式 详情参考 https://hooks.umijs.org/zh-CN/hooks/async#%E5%88%86%E9%A1%B5
type Pagination struct {
	PageSize uint `form:"pageSize" binding:"-" query:"pageSize" example:"20"`
	Current  uint `form:"current" binding:"-" query:"current" example:"1"`
}

func (p *Pagination) IsPagination() bool {
	return p.Current > 0 && p.PageSize > 0
}

//func MiddleWare() gin.HandlerFunc{
//	return func(ctx *gin.Context) {
//		if strBegin,ok:= ctx.GetQuery("beginTime");ok{
//			loc, err := time.LoadLocation("Local")
//			if err != nil {
//				ctx.JSON(http.StatusBadRequest, err.Error())
//				return
//			}
//			begin, err := time.ParseInLocation(timeLayout, strBegin, loc)
//			if err != nil {
//				ctx.JSON(http.StatusBadRequest, err.Error())
//				return
//			}
//			ctx.
//			ctx.Set("beginTime",begin.String())
//		}
//		ctx.Next()
//	}
//}

type BlockTimeSearch struct {
	BeginTime string `form:"beginTime" binding:"-" query:"beginTime" example:"14000000"`
	EndTime   string `form:"endTime" binding:"-" query:"endTime" example:"14000000"`
}

func (p *BlockTimeSearch) Convert() (int64, int64, error) {
	if p.IsBlockTimeSearch() {
		//转化为时间戳参数

		loc, err := time.LoadLocation("Local")
		if err != nil {
			return 0, 0, err
		}
		tmp1, err := time.ParseInLocation(timeLayout, p.BeginTime, loc)
		if err != nil {
			return 0, 0, err
		}
		tmp2, err := time.ParseInLocation(timeLayout, p.EndTime, loc)
		if err != nil {
			return 0, 0, err
		}
		return tmp1.Unix(), tmp2.Unix(), nil
	} else {
		return 0, 0, nil
	}
}

func (p *BlockTimeSearch) IsBlockTimeSearch() bool {
	return p.BeginTime != "" && p.EndTime != ""
}

type NameSearch struct {
	Name string `form:"name" binding:"-" query:"name" example:"草原"`
}
type ItemTypeSearch struct {
	Category string `form:"category" binding:"-" query:"category" example:"image"`
}
type TradeTypeSearch struct {
	Type string `form:"type" binding:"-" query:"type" example:"sales"`
}

func (p *NameSearch) IsNameSearch() bool {
	return p.Name != ""
}

func (p *ItemTypeSearch) IsItemTypeSearch() bool {
	return p.Category != ""
}

func (p *TradeTypeSearch) IsTradeTypeSearch() bool {
	return p.Type != ""
}
